# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: James Ross-Gowan <rossymiles@gmail.com>

_realname=mpv
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.33.0
pkgrel=2
pkgdesc="Video player based on MPlayer/mplayer2 (mingw-w64)"
url="https://mpv.io/"
arch=('any')
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libarchive"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libcdio"
         "${MINGW_PACKAGE_PREFIX}-libcdio-paranoia"
         "${MINGW_PACKAGE_PREFIX}-libdvdnav"
         "${MINGW_PACKAGE_PREFIX}-libdvdread"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libplacebo"
         "${MINGW_PACKAGE_PREFIX}-lua51"
         "${MINGW_PACKAGE_PREFIX}-pkg-config"
         "${MINGW_PACKAGE_PREFIX}-rubberband"
         "${MINGW_PACKAGE_PREFIX}-shaderc"
         "${MINGW_PACKAGE_PREFIX}-spirv-cross"
         "${MINGW_PACKAGE_PREFIX}-uchardet"
         "${MINGW_PACKAGE_PREFIX}-vapoursynth"
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "winpty")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-python-rst2pdf"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "perl"
             "python")
optdepends=("${MINGW_PACKAGE_PREFIX}-youtube-dl: for video-sharing websites playback")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/mpv-player/${_realname}/archive/v${pkgver}.tar.gz"
        "7c4465c.patch"::"https://github.com/mpv-player/mpv/commit/7c4465c.patch")
sha256sums=('f1b9baf5dc2eeaf376597c28a6281facf6ed98ff3d567e3955c95bf2459520b4'
            'SKIP')

# strip doesn't work well with the mpv.com wrapper, so strip manually instead
options=(!strip !emptydirs)

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # vo_gpu: placebo: update for upstream API changes
  patch -Np1 -i "${srcdir}/7c4465c.patch"

  [[ -x ./waf ]] || /usr/bin/python ./bootstrap.py
  sed -i 's:bin/env python$:bin/env python:' waf
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  DEST_OS=win32 \
  TARGET=${MINGW_CHOST} \
  PKG_CONFIG=${MINGW_PREFIX}/bin/pkg-config \
  CC=gcc PERL=/usr/bin/perl \
  AR=${MINGW_PREFIX}/bin/ar \
  WINDRES=${MINGW_PREFIX}/bin/windres \
  /usr/bin/python3 ./waf configure \
    --prefix=${MINGW_PREFIX} \
    --check-c-compiler=gcc \
    --enable-libmpv-shared \
    --enable-caca \
    --enable-cdda \
    --enable-d3d-hwaccel \
    --enable-d3d11 \
    --enable-dvdnav \
    --disable-egl-angle \
    --disable-egl-angle-lib \
    --enable-jpeg \
    --enable-lcms2 \
    --enable-libarchive \
    --enable-libbluray \
    --enable-lua \
    --enable-rubberband \
    --enable-shaderc \
    --enable-spirv-cross \
    --enable-uchardet \
    --enable-vapoursynth \
    --enable-vulkan \
    --out="${srcdir}/build-${MINGW_CHOST}"

  /usr/bin/python ./waf build
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"
  export PYTHON=${MINGW_PREFIX}/bin/python
  /usr/bin/python ./waf install --destdir="${pkgdir}"

  # Strip binaries manually
  ${MINGW_PREFIX}/bin/strip "${STRIP_BINARIES}" "${pkgdir}${MINGW_PREFIX}/bin/mpv.exe"
  ${MINGW_PREFIX}/bin/strip "${STRIP_BINARIES}" "${pkgdir}${MINGW_PREFIX}/bin/mpv.com"
  ${MINGW_PREFIX}/bin/strip "${STRIP_SHARED}" "${pkgdir}${MINGW_PREFIX}/bin/mpv-1.dll"

  # Move encoding-profiles.conf to share/doc alongside the example .conf files.
  # mpv doesn't search /etc for configuration on MinGW.
  mv "${pkgdir}${MINGW_PREFIX}/etc/mpv/"*.conf "${pkgdir}${MINGW_PREFIX}/share/doc/mpv/"

  # mpv needs winpty for key bindings to work on the terminal
  mv "${pkgdir}${MINGW_PREFIX}/bin/mpv.exe" "${pkgdir}${MINGW_PREFIX}/bin/mpv_exe"
  _exename=mpv
  echo '#!/usr/bin/env bash' > "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  echo 'export _started_from_console=yes' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_exename}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  mv "${pkgdir}${MINGW_PREFIX}/bin/mpv_exe" "${pkgdir}${MINGW_PREFIX}/bin/mpv.exe"
}
